package com.woniuxy.mq01.controller;

import com.woniuxy.mq01.entity.Student;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author author
 * @Create 2023-09-28 11:34
 */
@RestController
public class MyController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/test")
    public String sendtest() {
        //String exchange, String routingKey, Object message
//        for (int i=1;i<=10;i++) {
//            rabbitTemplate.convertAndSend("", "queuehelloworld", "msg-"+i);
//        }
        return "msg---ok";
    }
    @RequestMapping("/email")
    public String sendEmail() {
        //simplest发送
//        rabbitTemplate.convertAndSend("", "queueemail", "email");
        //routing发送
//        rabbitTemplate.convertAndSend("exchangedirect", "email", "email");
        //topics发送
        rabbitTemplate.convertAndSend("exchangetopic", "email", new Student(1,"zs",11));
        return "email---ok";
    }
    @RequestMapping("/exchange")
    public String sendExchange() {
        rabbitTemplate.convertAndSend("exchangetopicxxx", "email", new Student(1,"zs",11));
        return "email---ok";
    }
    @RequestMapping("/routing")
    public String sendRouting() {
        rabbitTemplate.convertAndSend("exchangetopic", "emailxxx", new Student(1,"zs",11));
        return "email---ok";
    }
    @RequestMapping("/sms")
    public String sendSms() {
        //simplest
//        rabbitTemplate.convertAndSend("", "queuesms", "sms");
        //routing发送
//        rabbitTemplate.convertAndSend("exchangedirect", "sms", "sms");
        //topics发送
        rabbitTemplate.convertAndSend("exchangetopic", "sms", "email");
        return "sms---ok";
    }
    @RequestMapping("/all")
    public String sendAll() {
        //使用simpleset模式，一个消息发送到多个队列，要发送多次
//        rabbitTemplate.convertAndSend("", "queueemail", "email");
//        rabbitTemplate.convertAndSend("", "queuesms", "sms");
        ////使用fanout模式，将消息发送到交换机，和和这个交换机绑定的所有队列都会收到消息
//        rabbitTemplate.convertAndSend("exchangefanout", "", "email-sms");

        //routing发送
//        rabbitTemplate.convertAndSend("exchangedirect", "email", "email");
//        rabbitTemplate.convertAndSend("exchangedirect", "sms", "sms");

        //topics发送
        rabbitTemplate.convertAndSend("exchangetopic", "email.sms", "email");
        return "all---ok";
    }
}
